//
//  CustomAnnotationView.m
//  HelloAmap
//
//  Created by xiaoming han on 14-10-21.
//  Copyright (c) 2014年 AutoNavi. All rights reserved.
//

#import "CustomAnnotationView.h"

#define kCalloutWidth       137
#define kCalloutHeight      50.0

#define kWidth  15.f
#define kHeight 15.f

@interface CustomAnnotationView ()

@property (nonatomic, strong, readwrite) CustomCalloutView *calloutView;
@property (nonatomic, strong) UIImageView *portraitImageView;

@end

@implementation CustomAnnotationView
@synthesize calloutView = _calloutView;

#pragma mark - Override

- (void)setSelected:(BOOL)selected
{ 
    [self setSelected:selected animated:NO];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.selected == selected)
    {
        return;
    }
    
    if (selected)
    {
        if (self.calloutView == nil)
        {
            self.calloutView = [[CustomCalloutView alloc] initWithFrame:CGRectMake(0, 0, kCalloutWidth, kCalloutHeight) title:self.annotation.title];
            self.calloutView.center = CGPointMake(CGRectGetWidth(self.bounds) / 2.f + self.calloutOffset.x,
                                                  -CGRectGetHeight(self.calloutView.bounds) / 2.f + self.calloutOffset.y);
        }
        
        self.calloutView.image = self.Logoimage;//logo
        self.calloutView.title = self.xiangMu_title;//名字
        self.calloutView.xiangMu_ID = self.xiangMu_id;//项目ID
        self.calloutView.PMzhi=self.PM; //Pm2.5;
        self.calloutView.Airzhi=self.AirString; //空气质量
        self.calloutView.xiangMu_tupe=self.tupe;  //类型
        self.calloutView.discountzhi=self.discount;// 活动图片
        self.calloutView.Businesszhi=self.bussines;
        self.calloutView.controller = self.controller;
        self.calloutView.imageURL = self.imageURL;
        self.calloutView.discountzhiURL = self.discountzhiURL;
        self.calloutView.Bussinslabel=self.busslabel;

        [self addSubview:self.calloutView];
    }
    else
    {
        [self.calloutView removeFromSuperview];
    }
    
    [super setSelected:selected animated:animated];
}


- (id)initWithAnnotation:(id<MAAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier];
    
    if (self)
    {
        self.bounds = CGRectMake(0.f, 0.f, kWidth, kHeight);
        self.portraitImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        [self addSubview:self.portraitImageView];

    }
    
    return self;
}

// 重新此函数，用以实现点击calloutView判断为点击该annotationView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    BOOL inside = [super pointInside:point withEvent:event];
    
    if (!inside && self.selected)
    {
        inside = [self.calloutView pointInside:[self convertPoint:point toView:self.calloutView] withEvent:event];
    }
    
    return inside;
}

//合作商场名字
- (void)setXiangMu_title:(NSString *)xiangMu_title{
    if (xiangMu_title != nil) {
        _xiangMu_title = xiangMu_title;
        //
        //        dispatch_async(dispatch_get_main_queue(), ^{
        
        self.calloutView.title = self.xiangMu_title;
        //        });
    }
}
//Loge 图片
-(void)setLogoimage:(UIImage *)Logoimage{

    if (Logoimage!=nil) {
        _Logoimage=Logoimage;
       self.calloutView.image = self.Logoimage;
    }
   
}

//PM值
-(void)setPM:(NSString *)PM{

    if (PM !=nil) {
        _PM=PM;
        self.calloutView.PMzhi=self.PM;
    }

}
//空气指数
-(void)setAirString:(NSString *)AirString{

    if (AirString!=nil) {
        _AirString=AirString;
       self.calloutView.Airzhi=self.AirString;
    }
}

//活动图
-(void)setDiscount:(UIImage *)discount{

    if (discount!=nil) {
        _discount=discount;
        self.calloutView.discountzhi=self.discount;
    }

}
//合作商家
-(void)setBussines:(NSString *)bussines{

    if (bussines!=nil) {
        _bussines=bussines;
        self.calloutView.Businesszhi=self.bussines;
    }
   
}


- (void)setDiscountzhiURL:(NSString *)discountzhiURL{
    if (discountzhiURL != nil) {
        _discountzhiURL = discountzhiURL;
        self.calloutView.discountzhiURL = self.discountzhiURL;
    }
}
- (void)setImageURL:(NSString *)imageURL{
    if (imageURL != nil) {
        _imageURL = imageURL;
        self.calloutView.imageURL = self.imageURL;
    }
}


-(void)setBusslabel:(UILabel *)busslabel{
    if (busslabel==nil) {
        _busslabel=busslabel;
        self.calloutView.Bussinslabel=self.busslabel;

    }


}

- (void)setAnotationImage:(UIImage *)anotationImage{
    self.portraitImageView.image = anotationImage;
}

- (UIImage *)anotationImage{
    return self.portraitImageView.image;
}

@end

